#include "mainwindow.h"

#include <QApplication>
#include <QDebug>
#include <QLabel>
#include <QLayout>
#include <QSizePolicy>

#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/keyboard.h>
#include <KWayland/Client/registry.h>

using namespace KWayland::Client;

static const QString s_socketName = QStringLiteral("kwin-test-wayland-seat-0");

class WaylandDisplayDemo
{
private:
    /* data */
public:
    WaylandDisplayDemo(/* args */);
    ~WaylandDisplayDemo();
};

WaylandDisplayDemo::WaylandDisplayDemo(/* args */)
{
}

WaylandDisplayDemo::~WaylandDisplayDemo()
{
}

void init()
{
}

int main(int argc, char *argv[])
{
    qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("wayland"));
    QApplication a(argc, argv);
    MainWindow w;
    w.init();

    ConnectionThread *connection = ConnectionThread::fromApplication();
    Registry registry;
    registry.create(connection);

    QObject::connect(
        &registry,
        &Registry::interfacesAnnounced,
        &a,
        [&registry, &w] {
            const bool hasKeyState = registry.hasInterface(Registry::Interface::Seat);
            QLabel *hasKeyStateLabel = new QLabel(&w);
            if (hasKeyState) {
                hasKeyStateLabel->setText(QStringLiteral("Compositor provides a KeyState"));
            } else {
                hasKeyStateLabel->setText(QStringLiteral("Compositor does not provide a KeyState"));
            }
            hasKeyStateLabel->adjustSize();

            Seat *keyState = nullptr;
            if (hasKeyState) {
                const auto keyStateInterface = registry.interface(Registry::Interface::Seat);
                keyState = registry.createSeat(keyStateInterface.name, keyStateInterface.version);

                // Keyboard *keyboard = keyState->createKeyboard(keyState);
                // QObject::connect(keyboard, &Keyboard::keyRepeatChanged, keyboard, [=](){
                //     qWarning() << "[=== checkmode ===]" << "repeat changed";
                // });
            }

            w.show();
        },
        Qt::QueuedConnection);
    QObject::connect(&registry, &Registry::shellAnnounced, &a, [&a, &registry](quint32 name, quint32 version) {
        Shell *m_shell = registry.createShell(name, version, &a);
    });
    registry.setup();

    return a.exec();
}
